<?php

declare(strict_types=1);

namespace App\Command;

use App\Model\CommodityModel\CommodityArchives;
use App\Model\CommodityModel\CommodityDescription;
use App\Model\CommodityModel\Stockinfo;
use App\Model\CommodityModel\SupplierCommodity;
use App\Service\CommodityService\SkuCodeBuilderService;
use Hyperf\Command\Command as HyperfCommand;
use Hyperf\Command\Annotation\Command;
use Psr\Container\ContainerInterface;
use Service\AppService\IdService;

/**
 * @Command
 */
class GoodsTask extends HyperfCommand
{
    /**
     * @var ContainerInterface
     */
    protected $container;

    public function __construct(ContainerInterface $container)
    {
        $this->container = $container;

        parent::__construct('GoodsTask');
    }

    public function configure()
    {
        parent::configure();
        $this->setDescription('生产测试商品');
    }

    public function handle()
    {
        $total = 500000;
        $count = 0;
        $commodity_goods = $supplier_goods = $commodity_detail_goods = $stock_data = [];
        $this->line('开始添加商品', 'info');
        for ($i = 0; $i < $total; $i++) {
            $goods = $this->getGoods();
            $commodity_goods[] = $goods['commodityarchivesData'];
            $supplier_goods[] = $goods['supplierGoodsData'];
            $commodity_detail_goods[] = $goods['commoditydescriptionData'];
            $stock_data[] = $goods['stockData'];
            $this->line($goods['supplierGoodsData']['SupplierCommodityName'] . '已经组装完成,count为' . $count, 'info');
            $count++;
            if ($count == 1000) {
                //添加商品
                CommodityArchives::instance()->insert($commodity_goods);
                SupplierCommodity::instance()->insert($supplier_goods);
                CommodityDescription::instance()->insert($commodity_detail_goods);
                Stockinfo::instance()->insert($stock_data);
                //添加完成后清空数据
                $count = 0;
                $commodity_goods = $supplier_goods = $commodity_detail_goods = $stock_data = [];
                $this->line('添加1000条成功', 'info');
            }
        }
        $this->line('数据添加完成', 'info');
    }

    private function getGoods()
    {
        $ca_id = IdService::instance()->getOtherID();
        $supplier_goods_id = IdService::instance()->getOtherID();
        $images_id = IdService::instance()->getOtherID();
        $skuCode = SkuCodeBuilderService::instance()->getCode();
        $stockID = IdService::instance()->getOtherID();
        $type = mt_rand(0, 2);
        $barCode = mt_rand(100, 9999999999999);

        $commodityarchivesData = [
            'CreateTime' => date('Y-m-d H:i:s'),
            'ID' => $ca_id,
            'MarketPrice' => '0.00',
            'SKUCode' => $skuCode,
            'CountryID' => '190840113021067268',
            'UnitID' => '199130779823923204',
            'Name' => $supplier_goods_id . '_商品',
            'BrandID' => '206115930747445252',
            'FirstClassificationID' => '305368685424412546',
            'SecondClassificationID' => '340521260024858498',
            'ThirdClassificationID' => '340521674988324738',
            'BarCode' => $barCode,
            'Weight' => 0.0000,
            'CommodityType' => $type,
            'IsExamine' => 1,
            'IsTryUpdateCommodityArchives' => 0,
            'FreightTemplateID' => 1,
            'IsIntegrity' => 1,
            'IsUseAble' => 1
        ];

        $commoditydescriptionData = [
            'CreateTime' => date('Y-m-d H:i:s'),
            'ID' => $images_id,
            'Description' => '<img src="http://space-16140.ushopn7.com/20205614191b74f5d72-b6e6-4a27-8439-b5aa89724558.jpg" alt="" /><img src="http://space-16140.ushopn7.com/20205614192a387651d-ae6f-4c0c-ba47-bc8ecffca7ed.jpg" alt="" /><img src="http://space-16140.ushopn7.com/202056141924c893daa-2699-4ec2-847d-73254fcded0d.jpg" alt="" /><img src="http://space-16140.ushopn7.com/2020561419350eb3eae-fe48-4cf2-906b-42a75d974819.jpg" alt="" /><img src="http://space-16140.ushopn7.com/202056141934e31f1f8-0e66-403a-9674-dab5ff7f83ea.jpg" alt="" />',
            'Images' => '[{"ImageUrl":"https://indoorbuydev.oss-cn-hangzhou.aliyuncs.com/images/supplierimg/1691032869729_2f5318ce.png","IsMain":1}]',
            'HasDes' => 1,
            'HasImage' => 1,
            'CommodityInfoID' => $ca_id
        ];



        $supplierGoodsData = [
            'CreateTime' => date('Y-m-d H:i:s'),
            'ID' => $supplier_goods_id,
            'CustomID' => 0,
            'TaxRate' => '0.091',
            'SupplierCommodityDescription' => '<p>1</p>',
            'SupplierCommodityUinit' => '个',
            'SupplierCommodityBrand' => 'vans',
            'SupplierCommodityClassify' => '鞋子',
            'SupplierCommodityOriginPlace' => '中国',
            'SupplierCommodityInternationBarCode' => $barCode,
            'SupplierCommodityType' => $type,
            'SupplierCommodityImageUrl' => '["https://indoorbuydev.oss-cn-hangzhou.aliyuncs.com/images/supplierimg/1692773924659_9cce0305.jpg"]',
            'SupplierCompanyID' => '749574684231598080',
            'SupplierCommoditySkuCode' => $supplier_goods_id,
            'SupplierCommodityName' => $supplier_goods_id . '_商品',
            'SupplierCommodityKey' => '',
            'SupplierCommodityNetWeight' => '0.0000',
            'SupplierCommoditySpec' => null,
            'SaleType' => 1,
            'SKUCode' => $skuCode, //
            'CommodityArchivesID' => $ca_id, //
            'HSCode' => '',
            'IsHaveTaxRate' => 1,
            'UpstreamSaleType' => 1,
            'LimitPriceMode' => 1,
            'LimitPriceScale' => 0.0000,
            'ConSumptionTaxRate' => 0.0000,
            'IsNeedWeiXinPay' => 0,
            'IsHasTag' => 0,
            'IsNeedUploadIDCard' => 0,
            'TagIDs' => null,
            'IsNeedIDCard' => $type ? 1 : 0,
            'IsNeedGoodsDown' => 0,
            'FreightTemplate' => null,
            'IsSyncFreight' => 0
        ];

        $stock = [
            'CreateTime' => date('Y-m-d H:i:s'),
            'ID' => $stockID,
            'IsDefective' => 0,
            'Number' => 100,
            'NumberLock' => 0,
            'Version' => 0,
            'BatchNo' => '基准批次',
            'BatchSpecMinSinglePrice' => 200,
            'BatchSpecMaxSinglePrice' => 200,
            'IsHasPrice' => 1,
            'SpecList' => '[{"SpecificationNum":"1.00","SpecificationPrice":"200.00"}]',
            'SupplierCompanyID' => '749574684231598080',
            'CommodityArchivesID' => $ca_id,
            'SupplierCommodityID' => $supplier_goods_id,
            'WarehouseID' => '749643076514873344',
            'LimitBuyCount' => 0,
            'BatchID' => 0
        ];

        return ['commodityarchivesData' => $commodityarchivesData, 'commoditydescriptionData' => $commoditydescriptionData, 'supplierGoodsData' => $supplierGoodsData, 'stockData' => $stock];
    }

    protected function getArguments()
    {
        return [];
    }
}
